package me.android.library.ui.config;

import android.os.Build;

import com.google.common.collect.Lists;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import lombok.Data;
import me.android.library.ui.api.MenuListener;


/**
 * Created by sylar on 15/6/3.
 */
@Data
public class UiConfig {

    protected String main;
    protected List<FormInfo> forms = Lists.newArrayList();
    protected List<MenuInfo> menus = Lists.newArrayList();
    protected List<PageInfo> pages = Lists.newArrayList();

    public UiConfig() {
    }

    public PageInfo getPageInfo(String pageKey) {
        return pages.stream().filter(page -> Objects.equals(pageKey, page.getId()))
                .findFirst().orElse(null);
    }

    public MenuListener getMenuListener(String menuKey) {
        MenuInfo mi = getMenuInfo(menuKey);
        return mi.get();
    }

    public MenuInfo getMenuInfo(String menuKey) {
        return menus.stream().filter(menu -> Objects.equals(menuKey, menu.getId()))
                .findFirst().orElse(null);
    }

    public List<MenuInfo> getValidMenus() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
            return menus.stream().filter(MenuInfo::isValid).collect(Collectors.toList());
        } else {
            return menus.stream().filter(MenuInfo::isValid).collect(Collectors.toList());
        }
    }

    public FormInfo getFormInfo(String formKey) {
        return forms.stream().filter(form -> Objects.equals(formKey, form.getId()))
                .findFirst().orElse(null);
    }
}
